package com.foreverwin.mes.lims.controller;

import com.alibaba.fastjson.JSONObject;
import com.foreverwin.mes.core.base.FrontPage;
import com.foreverwin.mes.core.base.AjaxResult;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.foreverwin.mes.lims.model.InactiveTime;
import com.foreverwin.mes.lims.service.InactiveTimeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.foreverwin.mes.lims.service.ItemTankService;
import com.foreverwin.mes.lims.model.ItemTank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;

/**
 *
 * @author Max
 * @since 2020-03-13
 */
@Controller
@RequestMapping("/itemTanks")
public class ItemTankController {
    private final Logger logger = LoggerFactory.getLogger(ItemTankController.class);

    @Autowired
    public ItemTankService itemTankService;
    @Autowired
    private InactiveTimeService inactiveTimeService;

    /**
    * 根据id查询
    *
    * @param id 主键
    * @return
    */
    @ResponseBody
    @GetMapping("/{id:.+}")
    public AjaxResult getItemTankById(@PathVariable String id) {
        ItemTank result;
        try {
            result = itemTankService.selectById(id);
        } catch (Exception e) {
            logger.error("getItemTankById -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(result);
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public AjaxResult getItemTankList(ItemTank itemTank){
        List<ItemTank> result;
        try {
            EntityWrapper<ItemTank> EntityWrapper = new EntityWrapper<>();
            EntityWrapper.setEntity(itemTank);
            result = itemTankService.selectList(EntityWrapper);
        } catch (Exception e) {
            logger.error("getItemTankList -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(result);
    }

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/getItemTankPageList")
    public AjaxResult getItemTankPageList(FrontPage<ItemTank> frontPage, ItemTank itemTank){
        Page result;
        try {
            String site = CommonMethods.getSite();
            EntityWrapper<ItemTank> EntityWrapper = new EntityWrapper<>();
            EntityWrapper.setEntity(itemTank);
            if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
                //TODO modify global query
                EntityWrapper
                    .like("handle", frontPage.getGlobalQuery())
                    .or().like("zInactiveTimeBo", frontPage.getGlobalQuery())
                    .or().like("resrce", frontPage.getGlobalQuery())
                    .or().like("resrceDesc", frontPage.getGlobalQuery())
                    .or().like("tankByUse", frontPage.getGlobalQuery())
                    .or().like("status", frontPage.getGlobalQuery())
        ;
            }
            result = itemTankService.selectPage(frontPage.getPagePlus(), EntityWrapper);
        } catch (Exception e) {
            logger.error("getItemTankPageList -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(result);
    }

    /**
     * 满罐确认
     * @param jsonObject  传递的实体
     * @return  null 失败  实体成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_save")
    public AjaxResult itemTankSave(@RequestBody JSONObject jsonObject) {
        String site = CommonMethods.getSite();
        String user = CommonMethods.getUser();
        String handle = jsonObject.getString("handle");
        String highOrLow = jsonObject.getString( "highOrLow");
        Date actionTime = jsonObject.getTimestamp("actionTime");
        ItemTank itemTank = itemTankService.selectById( handle );
        String tank = itemTank.getResrce();
        String  zInactiveTimeBo = itemTank.getzInactiveTimeBo();
        InactiveTime inactiveTime =inactiveTimeService.selectById( zInactiveTimeBo );
        String item =inactiveTime.getItem();
        int count;
        try {
            count = itemTankService.tankHighOrLow(site,tank,item,highOrLow,actionTime);
        } catch (Exception e) {
            logger.error("itemTankSave -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(count);
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id:.+}")
    public AjaxResult itemTankDelete(@PathVariable("id") String id){
        int count;
        try {
            count = itemTankService.deleteById(id) ? 1 : 0;
        } catch (Exception e) {
            logger.error("itemTankDelete -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success("count: " + count);
    }

    /**
     * 批量删除对象
     * @param ids 实体集合ID
     * @return  0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_batchDelete")
    public AjaxResult removeByIds(List<String> ids){
        int count;
        try {
            count = itemTankService.deleteBatchIds(ids) ? 1 : 0;
        } catch (Exception e) {
            logger.error("itemTankBatchDelete -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success("count: " + count);
    }
}